package cn.com.huzo.service.manufacture.impl;

import cn.com.huzo.model.entities.manufacture.CommissionProcessingContractLinesEntity;
import cn.com.huzo.repository.manufacture.CommissionProcessingContractHeaderRepository;
import cn.com.huzo.repository.manufacture.CommissionProcessingContractLinesRepository;
import cn.com.huzo.service.manufacture.CommissionProcessingContractLinesManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 加工承揽合同表明细管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("CommissionProcessingContractLinesManager")
public class CommissionProcessingContractLinesManagerImpl extends GenericManagerImpl<CommissionProcessingContractLinesEntity, String>
        implements CommissionProcessingContractLinesManager {

    @Resource(name = "CommissionProcessingContractHeaderRepository")
    private CommissionProcessingContractHeaderRepository _dao;

    @Resource(name = "CommissionProcessingContractLinesRepository")
    private CommissionProcessingContractLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<CommissionProcessingContractLinesEntity> fetchByHeaderKey(Long headerKey) throws Exception {
        return _dao_lines.fetchByHeaderKey(headerKey);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<CommissionProcessingContractLinesEntity> fetchLinesForCmtStatistics(List<CommissionProcessingContractLinesEntity> itemList) throws Exception {
        return _dao_lines.fetchLinesForCmtStatistics(itemList);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(List<CommissionProcessingContractLinesEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        if (itemList.isEmpty()) {
            return _result;
        }

        Integer rowsAffected = _dao_lines.batchUpdate(itemList);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(), "生产管理→委托加工合同明细", "合同号：" + itemList.get(0).getContractNo(), "更新");
        }

        _result.put("Amount", _dao.fetchAmountByKey(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }
}
